package com.example.demospring.service.assistant;

import com.example.demospring.entity.*;
import com.example.demospring.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ConsultingAuditService {
    @Autowired ConsultingAppointmentRepository appointmentRepo;
    @Autowired ConsultingAppointmentAuditRepository auditRepo;

    // 查询待审核的预约
    public List<ConsultingAppointment> listPending() {
        return appointmentRepo.findAll().stream()
                .filter(a -> "待审核".equals(a.getStatus())).collect(Collectors.toList());
    }

    // 审核
    public void review(Integer appointmentId, Integer assistantId, String result, String note) {
        ConsultingAppointment appointment = appointmentRepo.findById(appointmentId)
                .orElseThrow(() -> new RuntimeException("Appointment not found"));

        // 这里 result 可能是 "通过"/"拒绝"，映射到枚举
        if ("通过".equals(result)) {
            appointment.setStatus(ConsultingAppointment.Status.已安排);
        } else {
            appointment.setStatus(ConsultingAppointment.Status.已取消);
        }

        appointmentRepo.save(appointment);

        ConsultingAppointmentAudit audit = new ConsultingAppointmentAudit();
        audit.setAppointmentId(appointmentId);
        audit.setAssistantId(assistantId);
        audit.setAuditResult(result);
        audit.setNote(note);
        audit.setAuditedAt(new java.sql.Timestamp(System.currentTimeMillis()));
        auditRepo.save(audit);
    }
}